//-----------------------------------------------------------------------
// <copyright file="AddFile.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <summary>Adds the specified file as a Web Service Extension. (IIS6 or later).</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Web.ServiceExtension
{
    using System;
    using Microsoft.Sdc.Tasks.Configuration.Web;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Add the specified file as a web service extension (IIS6 or later)
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Web.ServiceExtension.AddFile
    ///     Path="path"
    ///     Description="description" 
    ///     Deletable="deletable"
    ///     GroupID="groupID" 
    ///     Permission="permission"/>]]></code>
    /// <para>where:</para>
    /// <para><i>path (Required)</i></para>
    /// <para>Fully specified path to the extension file.</para>
    /// <para><i>description</i></para>
    /// <para>A description for this extension.</para>
    /// <para><i>deletable</i></para>
    /// <para>Boolean value used to prevent or allow a Web service extension file from being deleted from the Web Service Extension Restriction List.</para>
    /// <para><i>groupID</i></para>
    /// <para>A unique text ID associated with one or more ISAPIs or CGIs required for enabling the group.</para>
    /// <para><i>permission</i></para>
    /// <para>Boolean value indicating if the web service extension file is disabled (false) or enabled (true) after the Web service is added.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Web.ServiceExtension.AddFile
    ///             Path="c:\myweb\myextensionservice.dll"
    ///             Description="My Extension Service"
    ///             Deletable="false"
    ///             GroupID="myext01"
    ///             Permission="true"
    ///        />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class AddFile : TaskBase
    {
        private string path;
        private bool deleteable;
        private string description;
        private string groupID = String.Empty;
        private string permission = String.Empty;
        private string machineName = "localhost";

        /// <summary>
        /// Machine to operate on
        /// </summary>
        public string MachineName
        {
            get { return this.machineName; }
            set { this.machineName = value; }
        }

        /// <summary>
        /// Path to the web extension service file.
        /// </summary>
        /// <value>Fully specified path to the web extension service file</value>
        [Required]
        public string Path
        {
            get { return (this.path ?? String.Empty); }
            set { this.path = value; }
        }

        /// <summary>
        /// Permissions flag to control if a Web service extension file can be 
        /// deleted from the Web Service Extension Restriction List.
        /// </summary>
        /// <value>"false" to prevent deletion though the IIS admin tools </value>
        /// <value>"true" to allow deletion</value>
        public bool Deleteable
        {
            get { return this.deleteable; }
            set { this.deleteable = value; }
        }

        /// <summary>
        /// Description of the web service extension being added
        /// </summary>
        /// <value>A valid web service description</value>
        public string Description
        {
            get { return (this.description ?? String.Empty); }
            set { this.description = value; }
        }

        /// <summary>
        /// A unique text ID associated with one or more ISAPIs or CGIs required for enabling the group
        /// </summary>
        /// <value>A unique, valid group ID</value>
        public string GroupID
        {
            get { return (this.groupID ?? String.Empty); }
            set { this.groupID = value; }
        }

        /// <summary>
        /// Flag indicating the web service extension start state.
        /// </summary>
        /// <value>"false" to start the extension as disabled, "true" to start as enabled</value>
        public string Permission
        {
            get { return (this.permission ?? String.Empty); }
            set { this.permission = value; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            ExtensionPermission exp = ExtensionPermission.Allowed;
            if (this.permission.Length > 0)
            {
                exp = (ExtensionPermission)Enum.Parse(typeof(ExtensionPermission), this.permission, true);
            }

            Log.LogMessageFromResources("Web.AddFileToWebServiceExtension", this.path);
            WebServiceExtension.AddFile(this.path, this.description, exp, this.deleteable, this.groupID, this.MachineName);
        }
    }
}